#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libwebsockets.h>

static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
                        void *user, void *in, size_t len)
{
    switch (reason) {
        case LWS_CALLBACK_RECEIVE:
            // 处理接收到的 WebRTC 信令数据
            printf("Received message: %s\n", (char *)in);
            // 在这里可以将信令消息转发到所有连接的客户端
            break;
        case LWS_CALLBACK_ESTABLISHED:
            printf("Connection established\n");
            break;
        case LWS_CALLBACK_CLOSED:
            printf("Connection closed\n");
            break;
        default:
            break;
    }

    return 0;
}

static struct lws_protocols protocols[] = {
    {
        "http",
        callback_http,
        0,
        1024,
    },
    { NULL, NULL, 0, 0 } /* End of protocols list */
};

int main() {
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof(info));

    info.port = 8080;  // WebSocket 服务器端口
    info.protocols = protocols;

    struct lws_context *context = lws_create_context(&info);
    if (!context) {
        printf("WebSocket server failed to start\n");
        return -1;
    }

    printf("WebSocket server started on ws://localhost:8080\n");

    while (1) {
        lws_service(context, 1000);  // 处理 WebSocket 事件
    }

    lws_context_destroy(context);
    return 0;
}